package com.rwb.reconciliation.service.impl;

import com.rwb.reconciliation.base.AbstractReconciliation;
import com.rwb.reconciliation.mapper.CheckIntMapper;
import com.rwb.reconciliation.service.IReconciliationService;
import com.rwb.reconciliation.sync.FutureFileParser;
import com.rwb.reconciliation.vo.ReconciliationParamVO;
import com.rwb.reconciliation.vo.ReconciliationResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 对账逻辑实现
 *
 * @Auther: ruwenbo
 * @Date: 2018/9/23 12:59
 * @Description:
 */
public class ReconciliationServiceImpl extends AbstractReconciliation implements IReconciliationService {

    private static final Logger logger = LoggerFactory.getLogger(ReconciliationServiceImpl.class);

    @Autowired
    private CheckIntMapper checkIntMapper;

    /**
     * 对账方式
     */
    @Value("checkType")
    private String checkType;

    private static final String FAIL_STATUS = "02";

    private static final String SUCCESS_STATUS = "01";

    private static ExecutorService executorService = Executors.newCachedThreadPool();

    /**
     * 核心非自主对账实现
     *
     * @param paramVO
     * @return
     */
    @Override
    public ReconciliationResultVO coreReconciliation(ReconciliationParamVO paramVO) {

        logger.info("################ 核心非自主对账(ReconciliationServiceImpl.coreReconciliation) start ################");
        logger.info("入参输出======>paramVO:{}", paramVO);

        return handleReconciliation(paramVO);
    }


    /**
     * 中国银联非自主对账实现
     *
     * @param paramVO
     * @return
     */
    @Override
    public ReconciliationResultVO unionChinalReconciliation(ReconciliationParamVO paramVO) {

        logger.info("################ 中国银联非自主对账(ReconciliationServiceImpl.unionChinalReconciliation) start ################");
        logger.info("入参输出======>paramVO:{}", paramVO);

        return handleReconciliation(paramVO);
    }


    private ReconciliationResultVO handleReconciliation(ReconciliationParamVO paramVO) {

        ReconciliationResultVO resultVO = new ReconciliationResultVO();

        if (super.checkParamVO(paramVO)) {
            logger.error("============== 入参错误 ================");
            resultVO.setStatus(FAIL_STATUS);
            resultVO.setErrMsg("入参错误");
            return resultVO;
        }

        try {
            /** 核心对账逻辑 */
            reconciliation(paramVO);
        } catch (Exception e) {
            resultVO.setStatus(FAIL_STATUS);
            resultVO.setErrMsg("非自主对账内部错误");
            return resultVO;
        }

        resultVO.setStatus(SUCCESS_STATUS);
        return resultVO;
    }

    /**
     * 核心对账逻辑处理
     *
     * @param paramVO
     */
    private void reconciliation(ReconciliationParamVO paramVO) throws Exception {

        // 1. 根据文件名, 文件类型校验该批次是否处理
        if (super.checkBatchFile(paramVO)) {
            logger.error("============== 对账已处理, 不再处理 ==============");
            return;
        }

        // 2. 开启两个线程(1.下载文件解析文件 2.删除本地流水表)
        Future<List<String>> fileContent = executorService.submit(new FutureFileParser(paramVO.getFileName()));

        executorService.execute(() -> {
            checkIntMapper.truncateCheckInt("472");
        });

        doReconciliation(fileContent.get(), checkType);
    }

    /**
     * 根据外部对账记录及本地流水对账
     *
     * @param fileContent
     * @param checkType   对账方式(1.内存对账 2.redis对账  3.数据库对账)
     */
    private void doReconciliation(List<String> fileContent, String checkType) {

        switch (checkType) {
            case "1":
                break;
            case "2":
                break;
            case "3":
                break;
            default:
                logger.error("==================== 未知的对账类型, 对账类型为:{} ==============", checkType);
                break;
        }
    }


}
